package com.jsj.controller;

import com.jsj.mapper.HistoryMapper;
import com.jsj.pojo.Device;
import com.jsj.pojo.DeviceHistory;
import com.jsj.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class DeviceController {

    @Autowired
    DeviceService deviceService;
    @Autowired
    HistoryMapper historyMapper;

//关键字搜索
    @RequestMapping("/queryDeviceByName")
    @ResponseBody
    public List<Device> queryDeviceByName(String type,String deviceName){
        List<Device> devicesList = deviceService.queryDeviceByName(type,deviceName);
        System.out.println("--queryDeviceByName---"+devicesList);
        return devicesList;
    }


//添加设备，在该类型下。
    @RequestMapping("/addDeviceByType")
    @ResponseBody
    public Boolean addDeviceByType(Device device){

        device.setDeviceStatus("正常");

        Boolean flag = deviceService.addDeviceByType(device);
        System.out.println("--flag---"+flag);
        return flag;
    }
//删除设备通过设备id
    @RequestMapping("/delDeviceById")
    @ResponseBody
    public Boolean delDeviceById(DeviceHistory deviceHistory){
        Boolean flag = deviceService.delDeviceById(deviceHistory.getDeviceId());
        Boolean aBoolean = historyMapper.addDeviceHistory(deviceHistory);
            if (flag=true){
                if (aBoolean=true){
                    return true;
                }
            }
        return false;
    }
//更新设备信息
    @RequestMapping("/updateDeviceById")
    @ResponseBody
    public Boolean updateDeviceById(int deviceId, String deviceType, String deviceName,
                    String deviceStatus, Float devicePrice, String storageTime,String deviceRemarks) throws ParseException {
        /*if (device.getStorageTime().isEmpty()){
            device.setStorageTime(null);
        }*/
        if (devicePrice==null) {
        devicePrice=0.0f;
        }
            Device device = new Device();
            device.setDeviceId(deviceId);
            device.setDeviceType(deviceType);
            device.setDeviceName(deviceName);
            device.setDeviceStatus(deviceStatus);
            device.setDevicePrice(devicePrice);
            device.setDeviceRemarks(deviceRemarks);
        Date storageTime2=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        storageTime2 = format.parse(storageTime);
        device.setStorageTime(storageTime2);

            Boolean flag = deviceService.updateDeviceById(device);
            System.out.println("--flag---"+flag);
            return flag;

    }




}
